//
//  SinusBarChartViewController.m
//  cocoapodText
//
//  Created by apple on 16/8/8.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ShiPei.h"
//导入时需要手动打上，系统没有提示
#import "Charts-Swift.h"
#import <Foundation/Foundation.h>
#import "SinusBarChartViewController.h"
@interface SinusBarChartViewController ()
@end

@implementation SinusBarChartViewController

BarChartView *sampleSinusBarChartView;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self initChartView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

-(void)initChartView{
    sampleSinusBarChartView=[[BarChartView alloc] initWithFrame:CGRectMake(kScreenW/4, kScreenH/4, kScreenW/2, kScreenH/2)];
    sampleSinusBarChartView.backgroundColor = [UIColor whiteColor];
    sampleSinusBarChartView.delegate = self;
    [self.view addSubview:sampleSinusBarChartView];
    [self initBarChartStyleAndData];
}


-(void)initBarChartStyleAndData{
    [self setupBarLineChartView:sampleSinusBarChartView];
    [self setBarChartParam:sampleSinusBarChartView];
    [self setLegendStyle:sampleSinusBarChartView];
    [self initChartData];
    [sampleSinusBarChartView animateWithXAxisDuration:2.0 yAxisDuration:2.0];
}

- (void)setupBarLineChartView:(BarChartView *)chartView
{
    chartView.descriptionText = @"descriptionText";
    chartView.noDataTextDescription = @"You need to provide data for the chart.";
    
    chartView.drawBarShadowEnabled = NO;
    chartView.drawValueAboveBarEnabled = YES;
    chartView.maxVisibleValueCount = 60;
    chartView.pinchZoomEnabled = NO;
    chartView.drawGridBackgroundEnabled = NO;
}

-(void)setBarChartParam:(BarChartView *)chartView{
    ChartXAxis *xAxis = chartView.xAxis;
    xAxis.labelPosition = XAxisLabelPositionBottom;
    xAxis.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:10.f];
    xAxis.drawGridLinesEnabled = NO;
    xAxis.enabled = NO;
    
    ChartYAxis *leftAxis = chartView.leftAxis;
    leftAxis.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:10.f];
    leftAxis.labelCount = 6;
    leftAxis.axisMinValue = -2.5;
    leftAxis.axisMaxValue = 2.5;
    leftAxis.granularityEnabled = true;
    leftAxis.granularity = 0.1;
    
    ChartYAxis *rightAxis = chartView.rightAxis;
    rightAxis.drawGridLinesEnabled = NO;
    rightAxis.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:10.f];
    rightAxis.labelCount = 6;
    rightAxis.axisMinValue = -2.5;
    rightAxis.axisMaxValue = 2.5;
    rightAxis.granularity = 0.1;
}

//设置图例样式
-(void)setLegendStyle:(BarChartView *)chartView{
    ChartLegend *l = chartView.legend;
    l.position = ChartLegendPositionBelowChartLeft;
    l.form = ChartLegendFormSquare;
    l.formSize = 9.0;
    l.font = [UIFont systemFontOfSize:11.f];
    l.xEntrySpace = 4.0;
}

- (void)initChartData
{
    [self setDataCount:150 chart:sampleSinusBarChartView];
}

- (void)setDataCount:(int)count chart:(BarChartView *)chartView
{
    NSMutableArray *xVals = [[NSMutableArray alloc] init];
    NSMutableArray *entries = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < count; i++)
    {
        [xVals addObject:[@(i) stringValue]];
        [entries addObject:[[BarChartDataEntry alloc] initWithValue:sinf(M_PI * (i % 128) / 64.0) xIndex:i]];
    }
    
    BarChartDataSet *set = nil;
    if (chartView.data.dataSetCount > 0)
    {
        set = (BarChartDataSet *)chartView.data.dataSets[0];
        set.yVals = entries;
        chartView.data.xValsObjc = xVals;
        [chartView notifyDataSetChanged];
    }
    else
    {
        set = [[BarChartDataSet alloc] initWithYVals:entries label:@"Sinus Function"];
        set.barSpace = 0.4;
        [set setColor:[UIColor colorWithRed:240/255.f green:120/255.f blue:124/255.f alpha:1.f]];
        
        BarChartData *data = [[BarChartData alloc] initWithXVals:xVals dataSet:set];
        [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:10.f]];
        [data setDrawValues:NO];
        
        chartView.data = data;
    }
}

#pragma mark - ChartViewDelegate

- (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry dataSetIndex:(NSInteger)dataSetIndex highlight:(ChartHighlight * __nonnull)highlight
{
    NSLog(@"chartValueSelected");
}

- (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
{
    NSLog(@"chartValueNothingSelected");
}

@end